﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IR.Controller.ModelControllers;
using IR.WPF.ViewModels.LandingViewModels;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;

namespace IR.WPF.UI.ViewModels
{
    public class VMIdea : ViewModelBase
    {
        private CIdea _idea;
        private ICommand _selectCommand;
        private bool _isSelected;
        private Button _attachedButton;

        public VMIdea(CIdea ideaToUse)
        {
            _idea = ideaToUse;
        }

        #region **Properties**
        public int IdeaId
        {
            get { return _idea.IdeaID; }
        }

        public string Version
        {
            get { return _idea.Version.VersionID; }
        }

        public string IdeaTitle
        {
            get { return _idea.Title; }
        }

        public CIdea IdeaController
        {
            get { return _idea; }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;

                // Change the color accordingly
                if (_isSelected == true)
                {
                    _attachedButton.Background = (Brush)new BrushConverter().ConvertFromString("Blue");
                }
                else
                {
                    _attachedButton.Background = (Brush)new BrushConverter().ConvertFromString("Yellow");
                }
            }
        }
        #endregion

        public string Overview
        {
            get { return (IdeaTitle + "\t" + Version); }
        }

        #region **Commands**
        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Select
        {
            get
            {
                if (_selectCommand == null)
                    _selectCommand = new RelayCommand(p => SelectFunction(p as Control), p => CanSelect(p as Control));

                return _selectCommand;
            }
        }
        #endregion

        #region **Command Functions**
        protected void SelectFunction(Control sender)
        {
            // Get a reference to the Window that contained the sender Control
            MainWindow currentMain = App.Current.MainWindow as MainWindow;

            _attachedButton = sender as Button;

            if (currentMain.CurrentlySelectedIdea == null)
            {
                currentMain.CurrentlySelectedIdea = this;
                IsSelected = true;
            }
            else
            {
                currentMain.CurrentlySelectedIdea.IsSelected = false;
                currentMain.CurrentlySelectedIdea = this;
                IsSelected = true;
            }
        }
        #endregion

        #region **CanRun Functions**
        protected bool CanSelect(Control sender)
        {
            return true;
        }
        #endregion
    }
}
